#include "../../include/http/HttpRequest.h"

namespace http
{
    void HttpRequest::setReceiveTime(muduo::Timestamp receive_time)
    {
        _receive_time = receive_time;
    }

    // 设置HTTP请求方法
    bool HttpRequest::setMethod(const char *start, const char *end)
    {
        std::string method(start, end);
        if (method == "GET")
        {
            _method = Get;
        }
        else if (method == "POST")
        {
            _method = Post;
        }
        else if (method == "PUT")
        {
            _method = Put;
        }
        else if (method == "DELETE")
        {
            _method = Delete;
        }
        else if (method == "HEAD")
        {
            _method = Head;
        }
        else if (method == "OPTIONS")
        {
            _method = Options;
        }
        else
        {
            _method = Invalid;
            return false;
        }
        return true;
    }

    // 设置请求路径
    bool HttpRequest::setPath(const char *start, const char *end)
    {
        _path.assign(start, end);
        return true;
    }

    // 设置查询参数
    bool HttpRequest::setQuery(const char *start, const char *end)
    {
        std::string query_string(start, end);
        // 解析查询参数 key1=value1&key2=value2
        size_t pos = 0;
        while (pos < query_string.length())
        {
            size_t eq_pos = query_string.find('=', pos);
            if (eq_pos == std::string::npos)
                break;

            size_t and_pos = query_string.find('&', eq_pos);
            if (and_pos == std::string::npos)
                and_pos = query_string.length();

            std::string key = query_string.substr(pos, eq_pos - pos);
            std::string value = query_string.substr(eq_pos + 1, and_pos - eq_pos - 1);
            _query[key] = value;

            pos = and_pos + 1;
        }
        return true;
    }

    // 设置路径参数
    void HttpRequest::setPathParams(const std::string &key, const std::string &value)
    {
        _path_params[key] = value;
    }


    // 设置请求头
    void HttpRequest::addHeader(const char *start, const char *colon, const char *end)
    {
        // 提取并处理header name
        std::string key(start, colon);
        // 去除header name尾部的空格
        while (!key.empty() && isspace(key[key.size() - 1]))
        {
            key.resize(key.size() - 1);
        }

        // 移动到冒号后面
        ++colon;
        // 跳过冒号后面的空格
        while (colon < end && isspace(*colon))
        {
            ++colon;
        }

        // 提取header value
        std::string value(colon, end);
        // 去除header value尾部的空格
        while (!value.empty() && isspace(value[value.size() - 1]))
        {
            value.resize(value.size() - 1);
        }

        // 存储到headers map中
        _headers[key] = value;
    }

    // 获取指定的请求头值
    std::string HttpRequest::getHeader(const std::string &key) const
    {
        auto it = _headers.find(key);
        if (it != _headers.end())
        {
            return it->second;
        }
        return ""; // 如果找不到，返回空字符串
    }

    // 设置请求体
    bool HttpRequest::setContent(const char *start, const char *end)
    {
        _content.assign(start, end);
        _content_length = _content.length();
        return true;
    }

    void HttpRequest::setContentLength(size_t length)
    {
        _content_length = length;
    }
    void HttpRequest::swap(HttpRequest &other)
    {
        std::swap(_method, other._method);
        std::swap(_version, other._version);
        std::swap(_path, other._path);
        std::swap(_path_params, other._path_params);
        std::swap(_query, other._query);
        std::swap(_receive_time, other._receive_time);
        std::swap(_headers, other._headers);
        std::swap(_content, other._content);
        std::swap(_content_length, other._content_length);
    }
}